package com.itmuch.cloud.controller;

import com.itmuch.cloud.dao.UserRepository;
import com.itmuch.cloud.dto.UserDTO;
import com.itmuch.cloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        User findOne = userRepository.findById(id).orElse(null);
        return findOne;
    }

    /**
     *
     */
    @GetMapping("/user/{id}/orders/{orderId}")
    public String getOrder(@PathVariable("id") Long userId, @PathVariable("orderId") Long orderId) {
        return "userId:" + userId + " orderId:" + orderId;
    }

    @GetMapping("/user/search")
    String search(@RequestParam("name") String name, @RequestParam("age") Integer age, @RequestParam("size") Integer size) {
        return "name:" + name + " age:" + age + " size:" + size;
    }

    @PostMapping("/user/create")
    String createUser(@RequestBody UserDTO user) {
        return "name:" + user.getName() + " age:" + user.getAge() + " size:" + user.getSize();
    }

    /**
     * 单文件接收
     *
     * @param file
     * @param description
     * @return
     */
    @PostMapping("/user/upload")
    public String handleUpload(@RequestPart("file") MultipartFile file, @RequestPart("description") String description) {
        // 处理文件（保存到本地或云存储）
        String fileName = file.getOriginalFilename();
        return "文件 " + fileName + " 上传成功，描述：" + description;
    }
}
